package com.excesys.exsecs.om.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.admin.api.entity.SysUser;
import com.excesys.exsecs.admin.api.feign.RemoteUserService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.dto.OrderDto;
import com.excesys.exsecs.om.dto.OrderHistoryDto;
import com.excesys.exsecs.om.entity.OmWOrder;
import com.excesys.exsecs.om.service.OmWOrderService;
import com.excesys.exsecs.om.utils.WebSocketSend;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 保养工单
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ommaintenanceorder")
@Tag(name = "保养工单表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmMaintenanceOrderController {

	private final OmWOrderService omWOrderService;

	private final WebSocketSend webSoketTest;

	private final RemoteUserService userService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omWOrder 工单
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	// @PreAuthorize("@pms.hasPermission('om_omrepairworder_get')" )
	public R getOmWOrderPage(Page page, OmWOrder omWOrder, String flag) {
		return R.ok(omWOrderService.getMintenanceOrderWithRelPage(page, omWOrder, flag));
	}

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omWOrder 工单
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询工单代办")
	@GetMapping("/getPage")
	// @PreAuthorize("@pms.hasPermission('om_omworder_get')" )
	public R getPage(Page page, OmWOrder omWOrder) {
		return R.ok(omWOrderService.getMaintenancePage(page, omWOrder));
	}

	/**
	 * 通过id查询工单
	 * @param orId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{orId}")
	// @PreAuthorize("@pms.hasPermission('om_omrepairworder_get')" )
	public R getById(@PathVariable("orId") Integer orId) {
		return R.ok(omWOrderService.getById(orId));
	}

	/**
	 * 新增工单
	 * @param orderDto 工单
	 * @return R
	 */
	@Operation(summary = "新增工单", description = "新增工单")
	@SysLog("新增工单")
	@PostMapping
	// @PreAuthorize("@pms.hasPermission('om_omworder_add')" )
	public R save(@RequestBody OrderDto orderDto) {
		if ("2".equals(orderDto.getAppointType())) {
			webSoketTest.onMessage(null, orderDto.getHandleUserName() + "/你有一个新的工单的工单待处理请注意查收!!!");
		}
		return R.ok(omWOrderService.saveMaintenanceOmWOrder(orderDto));
	}

	/**
	 * 修改工单
	 * @param orderDto 工单
	 * @return R
	 */
	@Operation(summary = "修改工单", description = "修改工单")
	@SysLog("修改工单")
	@PutMapping
	// @PreAuthorize("@pms.hasPermission('om_omworder_edit')" )
	public R updateById(@RequestBody OrderDto orderDto) {

		return R.ok(omWOrderService.updateOmWOrder(orderDto));
	}

	/**
	 * 通过id删除工单
	 * @param orId id
	 * @return R
	 */
	@Operation(summary = "通过id删除工单", description = "通过id删除工单")
	@SysLog("通过id删除工单")
	@DeleteMapping("/{orId}")
	// @PreAuthorize("@pms.hasPermission('om_omworder_del')" )
	public R removeById(@PathVariable Integer orId) {

		return R.ok(omWOrderService.removeOmWOrderById(orId));
	}

	/**
	 * 通过typeName自动获取当前新的工单名称
	 * @param typeName
	 * @return R
	 */
	@Operation(summary = "通过typeName自动获取当前新的工单名称", description = "通过typeName自动获取当前新的工单名称")
	@GetMapping("/getCurrentOrderNameSeq/{typeName}")
	// @PreAuthorize("@pms.hasPermission('om_omworder_get')" )
	public R getCurrentOrderNameSeq(@PathVariable("typeName") String typeName) {
		return R.ok(omWOrderService.getCurrentOrderNameSeq(typeName));
	}

	/**
	 * 操作工单
	 * @param orderHistoryDto 工单
	 * @return R
	 */
	@Operation(summary = "操作工单", description = "操作工单")
	@SysLog("操作工单")
	@PostMapping("/operate")
	// @PreAuthorize("@pms.hasPermission('om_omworder_add')" )
	public R operateOmWOrder(@RequestBody OrderHistoryDto orderHistoryDto) {
		OmWOrder omWOrder = omWOrderService.getByOrderNo(orderHistoryDto.getOrderNo());
		if (ObjectUtil.isNotEmpty(omWOrder)) {
			if ("1".equals(orderHistoryDto.getOrderStatus())) {
				R<SysUser> userInfo = userService.getUserById(orderHistoryDto.getHandleUserId());
				webSoketTest.onMessage(null, userInfo.getData().getUsername() + "/你有一个新的工单的工单待处理请注意查收!!!");
			}
		}
		return R.ok(omWOrderService.operateOmWOrder(orderHistoryDto));
	}

	/**
	 * 新增巡检记录
	 * @param idList 巡检记录
	 * @return R
	 */
	@Operation(summary = "删除多个工单", description = "删除多个工单")
	@SysLog("删除多个工单")
	@DeleteMapping("/removeByIds")
	// @PreAuthorize("@pms.hasPermission('om_omworder_del')" )
	public R removeByIds(@RequestBody List<Integer> idList) {
		return R.ok(omWOrderService.removeByIds(idList));
	}

}
